<?php
/**
 * Class:   Setting
 * About:   Модель - Настройка
 * Author:  Vlad Andreev <php.vlad@gmail.com>
 **/

namespace Wysiwygs;

class Wysiwyg extends \Control\Model
{
    public function __construct()
    {
        parent::__construct();
        
        $this->repository = $this->em->getRepository('Wysiwygs\Entities\Wysiwyg');
    }
    
    public function save($data, $id = null)
    {
        if(!empty($id))
            $item = $this->find($id);
        
        if(empty($item))
            $item = new \Wysiwygs\Entities\Wysiwyg;
        
        $entityFields = array_keys(get_object_vars($item));
        
        $data['active'] = (!empty($data['active'])) ? 1 : 0;
        
        foreach ($data as $field => $value)
            if (in_array($field, $entityFields))
                $item->$field = $value;

        $this->em->persist($item);
        $this->em->flush();
        
        return $item->id;
    }
    
    public function setAccess() {
        
        $live = \Control\Live::getInstance();
        
        if ($live->user->access(array('module' => 'Wysiwygs', 'action' => 'browse'))) {
            $_SESSION['KCFINDER'] = array();
            $_SESSION['KCFINDER']['disabled'] = false;
        }
    }
}